package com.library.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.library.dao.DAOFactory;
import com.library.dao.MySQLDAOFactory;
import com.library.dao.User;
import com.library.dao.UserDAO;

/**
 * Servlet implementation class UserChangePassword
 */
@WebServlet("/UserChangePassword")
public class UserChangePassword extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserChangePassword() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String oldPassword = request.getParameter("oldpassword");
		String newPassword = request.getParameter("newpassword");
		String retype = request.getParameter("retype");
		
		HttpSession session = request.getSession();
		User instantUser = (User)session.getAttribute("instantUser");
		
		if (!instantUser.getPassword().equals(oldPassword)){
			// go sai mat khau cu
		} else {
			// go dung mat khau cu
			if (!newPassword.equals(retype)){
				// go sai retype
			} else{
				// go dung can cap nhat lai 
				instantUser.setPassword(newPassword);
				
				DAOFactory factory = new MySQLDAOFactory();
				UserDAO userDAO = factory.getUserDAO();
				userDAO.updateUser(instantUser);
				
				String address = "user/userchangepasswordsucess.jsp";				
				 RequestDispatcher dispatcher =
					      request.getRequestDispatcher(address);
					    dispatcher.forward(request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
